package com.ourmovieguy;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {

	static String search_base = "http://api.nytimes.com/svc/movies/v2/reviews/search.xml?&response-format=XML&api-key=70b71382c2a57dd101080000f0b1f3e9:4:59939697&query=";

	
	static Document getXmlStringFromQuery(String query) throws IOException, SAXException, ParserConfigurationException {		
		String term = query.replace(" ", "+");
		String searchurl = search_base + term;
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//Using factory get an instance of document builder
		DocumentBuilder db = dbf.newDocumentBuilder();
		
		 URL url = new URL(searchurl);
		 Document dom = db.parse(url.openStream());
		 return dom;
	}
	
	static String StringFromTag(Element el, String tagname) {
		NodeList nl = el.getElementsByTagName(tagname);
		Element el1 = (Element)nl.item(0);
		if (null != el1.getFirstChild()) {
			return el1.getFirstChild().getNodeValue();
		} else {
			return "";
		}
	}
	
	static NYTMovie movieFactory(Element reviewElement) {
		
		String url = "";
		NodeList nl = reviewElement.getElementsByTagName("url");
		
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(1);
			if (null != el.getFirstChild()) {
				url =  el.getFirstChild().getNodeValue();
			}
		}
		
		NYTMovie movie = new NYTMovie(
				StringFromTag(reviewElement, "display_title"),
				StringFromTag(reviewElement, "mpaa_rating"),
				StringFromTag(reviewElement, "publication_date"),
				StringFromTag(reviewElement, "capsule_review"),
				url);
		return movie;
	}
	
	static List<NYTMovie> movieFromXmlDocument(Document dom) {
		List<NYTMovie> movies = new ArrayList<NYTMovie>();
		
		//get the root elememt
		Element docEle = dom.getDocumentElement();
		//get a nodelist of <employee> elements
		NodeList nl = docEle.getElementsByTagName("review");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				Element el = (Element)nl.item(i);
				movies.add(movieFactory(el));
			}
		}
		
		
		return movies;
	}
		
	/**
	 * @param args
	 * @throws ParserConfigurationException 
	 * @throws SAXException 
	 */
	public static void main(String[] args) throws SAXException, ParserConfigurationException {
		 try {
			 
			 Document dom = getXmlStringFromQuery("planet of the apes");
			 for (NYTMovie e : movieFromXmlDocument(dom)) {
				 System.out.print("Movie:" + e.toString() + "\n"); 
			 }
			 
		
	        } catch (MalformedURLException e) {
	        	System.out.print("Error1");
	        } catch (IOException e) {
	        	System.out.print("Error2" + e.toString());
	        }
		
	}

}
